package com.choosefine.it.worker.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.choosefine.it.base.BaseService;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.basedata.dao.WorkTypeMapper;
import com.choosefine.it.common.utils.IdsUtil;
import com.choosefine.it.worker.dao.WorkerItemsRelationMapper;
import com.choosefine.it.worker.dao.WorkerWorkTypeMapper;
import com.choosefine.it.worker.model.WorkerItemsRelation;
import com.choosefine.it.worker.model.dto.WorkerItemsRelationDTO;
import com.choosefine.it.worker.model.dto.WorkerItemsRelationUpdateDTO;
import com.choosefine.it.worker.model.vo.WorkerItemsRelationFindVO;
import com.choosefine.it.worker.model.vo.WorkerItemsRelationVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
/**
 * 11.25 dzq 
 * @author Administrator
 *
 */
@Service("workerItemsRelationService")
public class WorkerItemsRelationService{
	
	@Autowired
	WorkerItemsRelationMapper workerItemRelationMapper;
	@Autowired
	WorkerWorkTypeMapper workerWorkTypeMapper;
	
	@Autowired
	MongoTemplate mongoTemplate;
	@Autowired
	WorkTypeMapper workTypeMapper;
	

	/**
	 * @author chenjian
	 * @time 2016-12-16 下午7:53:07
	 */
	public int deleteWorkerItemsRelation(WorkerItemsRelation workerItemsRelation) {
		// TODO 推送给租户  租户和班组已删除关系 传递班组编号taemId可以查看详情 [您已离开"下沙建筑公司--龙湖滟澜山--陈红具班组",点击查看详情....] 
		return workerItemRelationMapper.deleteWorkerItemsRelation(workerItemsRelation);
	}

	/**
	 * @author chenjian
	 * @time 2016-12-16 下午7:53:11
	 */
	public Page<WorkerItemsRelationVO> findWorkerByTeam(PageDTO pageDTO,
			Map<String, Object> params) {
		PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
		Page<WorkerItemsRelationVO> page = workerItemRelationMapper.findWorkerByTeam(params);
		return page;
	}

	/**
	 * @author chenjian
	 * @time 2016-12-21 下午7:14:44
	 */
	public List<String> findMemberByTeamId(Integer teamId) {
		return workerItemRelationMapper.findMemberByTeamId(teamId);
	}

	public int saveWorkItemsRelation(WorkerItemsRelationDTO workerItemsRelationDTO) {
		int num = workerItemRelationMapper.saveWorkItemsRelation(workerItemsRelationDTO);
		// TODO 推送给租户  租户和班组已建立关系 传递班组编号taemId可以查看详情 [您已加入"下沙建筑公司--龙湖滟澜山--陈红具班组",点击查看详情....] 
		return num;
	}

//	public int updateWorkerTypes(List<WorkerItemWorkTypeDTO> workerItemWorkTypeDTOs) {
//		int num = workerItemRelationMapper.updateWorkerTypes(workerItemWorkTypeDTOs);
//		// TODO 推送给租户  租户和班组已建立关系 传递班组编号taemId可以查看详情 [您已加入XXX班组,点击查看详情....] 
//		for (WorkerItemWorkTypeDTO workerItemsRelationDTO : workerItemWorkTypeDTOs) {
//			
//		}
//		return num;
//	}
	
	/**
	 * @Description：更新工人信息
	 * @Author：chenjian
	 * @Create Date：2017年2月16日
	 * @param：
	 * @return：
	 */
	public int updateWorkerTypes(WorkerItemsRelationUpdateDTO workerItemsRelationUpdateDTO) {
		workerWorkTypeMapper.delWorkerWorkTypeList(workerItemsRelationUpdateDTO.getId());
		int num = workerItemRelationMapper.updateWorkerTypes(workerItemsRelationUpdateDTO);
		
		List<Integer> list = IdsUtil.getIds(workerItemsRelationUpdateDTO.getWorkerTypeIdes());
		Map<String, Object> paras=new HashMap<>();
		paras.put("workTypeIdes", list); 
		paras.put("workerItemsRelationId",workerItemsRelationUpdateDTO.getId());
		workerWorkTypeMapper.saveWorkerWorkType(paras);
		
		Query query = new Query(Criteria.where("id").is(workerItemsRelationUpdateDTO.getId()));
		Update update = new Update().set("workTypeList", workTypeMapper.findWorkTypeListByIdes(paras)).set("workTime", workerItemsRelationUpdateDTO.getWorkTime());
		//Mongodb存储起来
		mongoTemplate.updateMulti(query, update, WorkerItemsRelation.class);
		
		return num;
	}

	public int findMemberByUserCodeAndTeamId(Map<String, Object> params) {
		return workerItemRelationMapper.findMemberByUserCodeAndTeamId(params);
	}
	public Integer selectByProjectCodeAndTeamId(Map<String, Object> params){
		return workerItemRelationMapper.selectByProjectCodeAndTeamId(params);
	}

	/**
	 * 
	 * @Description：工人详情
	 * @Author：chenjian
	 * @Create Date：2017年2月23日
	 * @param：
	 * @return：
	 */
	public WorkerItemsRelationFindVO findWorker(Integer id) {
		WorkerItemsRelationFindVO workerItemsRelationFindVO = workerItemRelationMapper.findWorker(id);
		//获取工人工种集合信息
		workerItemsRelationFindVO.setWorkTypeList(workerWorkTypeMapper.findWorkTypeList(workerItemsRelationFindVO.getId()));
		return workerItemsRelationFindVO;
	}
}